package com.cn.lf.wx.dacs.bussiness.author.serviceImpl;

import java.util.List;
import java.util.regex.Pattern;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cn.lf.wx.dacs.bussiness.author.bean.BaseException;
import com.cn.lf.wx.dacs.bussiness.author.bean.ReturnEntity;
import com.cn.lf.wx.dacs.bussiness.author.dao.AuthMenuMapper;
import com.cn.lf.wx.dacs.bussiness.author.entity.AuthMenu;
import com.cn.lf.wx.dacs.bussiness.author.entity.AuthRoleMenu;
import com.cn.lf.wx.dacs.bussiness.author.utils.StringUtil;


/**
 * <p>
 * 菜单列表 服务实现类
 * </p>
 *
 * @author jobob
 * @since 2022-03-01
 */
@Service
public class AuthMenuServiceImpl extends ServiceImpl<AuthMenuMapper, AuthMenu>  {
	@Value("${nxpcs.default.super.admin.id}")
	protected String superAdminId;

	@Autowired
	private AuthRoleMenuServiceImpl authRoleMenuService;

	    public ReturnEntity menuListAll() {
	        List<AuthMenu> list = list();
	        return ReturnEntity.SUCCESS("所有菜单获取成功.").setData(list);
	    }

	    public ReturnEntity createMenu(AuthMenu menu) {
	        save(menu);
	        return ReturnEntity.SUCCESS("菜单创建成功.");
	    }

	    public ReturnEntity modifyMenu(AuthMenu menu) {
	        if (!StringUtils.hasText(menu.getMid())) {
	            throw new BaseException("菜单ID为空.");
	        }
	        updateById(menu);
	        return ReturnEntity.SUCCESS("菜单修改成功.");
	    }

	    @Transactional(value="transactionManager")
	    public ReturnEntity deleteMenu(AuthMenu menu) {
	        String mid = menu.getMid();
	        if (!StringUtils.hasText(mid)) {
	            throw new BaseException("菜单ID为空.");
	        }
	        if ("0".equals(mid)) {
	            throw new BaseException("菜单ID不能为0.");
	        }
	        if (!Pattern.matches("^-?\\d+$",mid)) {
	            throw new BaseException("菜单ID格式错误.");
	        }
//	        判断是否在使用中
	        long roleUseMenuCount = authRoleMenuService.count(new LambdaQueryWrapper<AuthRoleMenu>().eq(AuthRoleMenu::getMenuId, mid));
	        if (roleUseMenuCount > 0) {
	            throw new BaseException("该菜单被角色使用中.");
	        }
	        LambdaQueryWrapper<AuthMenu> queryWrapper = new LambdaQueryWrapper<>();
	        queryWrapper.eq(AuthMenu::getMid, mid).or().like(AuthMenu::getPline,String.format("\"%s\"",mid));
	        remove(queryWrapper);
	        return  ReturnEntity.SUCCESS("菜单删除成功.");
	    }

	    public ReturnEntity menusForRole(String role) {
	        if (StringUtil.isEmpty(role)) {
	            return ReturnEntity.FAILED("角色不能为空");
	        }
	        List<AuthMenu> adminMenus;
//	        超级管理员 具有所有权限
	        if (superAdminId.equals(role)) {
	            adminMenus = list(new QueryWrapper<AuthMenu>().orderByDesc("sort"));
	        } else {
	            adminMenus = baseMapper.menusForRole(role);
	        }
	        return ReturnEntity.SUCCESS("获取成功").setData(adminMenus);
	    }
}
